<?php
class EmployeeCertificateSearch extends CFormModel {
	public $grantDateMin;
	public $grantDateMax;
	public $name;
	public $types;

	public function rules() {
		return array(
			array(
				'grantDateMin, grantDateMax',
				'date',
				'format' => 'yyyy-MM-dd',
			),
			array(
				'name',
				'length',
				'max' => 255
			),
			array(
				'types',
				'type',
				'type' => 'array'
			),
		);
	}

	public function attributeLabels() {
		return array(
			'types' => '证件类型',
			'name' => '姓名',
			'grantDateMin' => '获证日期'
		);
	}

	/**
	 * @return CDbCriteria
	 */
	public function createCriteria() {
		$criteria = new CDbCriteria;
		$criteria->with = array(
			'employee',
		);
		if ($this->name) {
			$criteria->addSearchCondition('CONCAT_WS(\'\',familyName, givenName)', $this->name);
		}

		if ($this->grantDateMin) {
			$criteria->compare('grantDate', '>=' . $this->grantDateMin);
		}
		if ($this->grantDateMax) {
			$criteria->compare('grantDate', '<=' . $this->grantDateMax);
		}
		if (!empty($this->types)) {
			$criteria->addInCondition('typeId', $this->types);
		}
		$criteria->order = 'grantDate DESC';

		return $criteria;
	}
}
